package com.intpro.demo.Service;

import com.intpro.demo.DAO.Application;
import com.intpro.demo.DAO.ApplyType;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

@Service
public class ApplyService
{

	@Autowired
	MongoTemplate mongoTemplate;

	public void addApp(Application a)
	{
		a.date = new Date();
		mongoTemplate.insert(a);
	}

	public Application passApp(long aid, boolean pass)
	{
		Query q1 = new Query(Criteria.where("id").is(aid));
		Application a = mongoTemplate.findOne(q1, Application.class);

		a.pass = pass;
		a.todo = false;

		mongoTemplate.save(a);
		return a;
	}

	public List<Application> findAllAppTodo(ApplyType t)
	{
		Query q1 = new Query(Criteria.where("applytype").is(t).and("todo").is(true));
		return mongoTemplate.find(q1, Application.class);
	}

	public List<Application> findAllAppTodo()
	{
		Query q1 = new Query(Criteria.where("todo").is(true));
		return mongoTemplate.find(q1, Application.class);
	}

	public Application findByAid(long aid)
	{
		Query q1 = new Query(Criteria.where("id").is(aid));
		return mongoTemplate.findOne(q1, Application.class);
	}

}
